package server;


import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class ReceiveThread extends Thread {
//	Socket connectionSocket ;
	BufferedReader inFromClient ;	
//	DataOutputStream outToClient ;
	DataOutputStream[] outs ;
	Buffer[] buffers;
	
	public ReceiveThread(Socket connectionSocket_var, DataOutputStream[] out_var , Buffer[] b) throws IOException{
//		System.out.println("\nnew ReceiveThread for host" + connectionSocket_var.getInetAddress());
		
		Socket connectionSocket = connectionSocket_var;		
		inFromClient = 
			new BufferedReader(new 
					InputStreamReader(connectionSocket.getInputStream()));
//		outToClient = 
//			new DataOutputStream(connectionSocket.getOutputStream());
//		outs = Server.out;
		outs = out_var;
		buffers = b;
	}
	
	public void run(){
		synchronized (outs) {
			int i = 0;
			try{
				while(true){
//					String msgToClient = "Server's thread "+ this.getId() +" i=" + i++ + '\n';
//					outs[0].writeBytes(msgToClient);
//					Server.buffers[0].queue.add(msgToClient);
//					buffers[0].queue.add(msgToClient);
					
					String clientMsg = inFromClient.readLine();
					
					if(clientMsg !=null){
						System.out.println("receive: " + clientMsg);// + " from host " + connectionSocket.getInetAddress());
						if(clientMsg.equals("req_STOP")){
							this.interrupt();
							break;
						}
						else if(!clientMsg.equals("req_INIT")){
							String[] x = clientMsg.split("\\_");
							String dest = x[1];
//							System.out.println("request info of: " + dest);
							route(clientMsg , dest);
						}			
					}
					
					Thread.sleep(100);
				}
			}
			catch (Exception e) { 
				e.printStackTrace();
				this.interrupt();
			}
		}
		
	}
	
	synchronized void route(String msg, String destAdd){
//		String server = Server.getServerAddr(destAdd);
		String server = destAdd;//pour tester
		if(server.equals(Server.localAddr)){//I will serve it
			System.out.println("I will serve it");
			//forward to destination host
//			Socket sk = Server.getSocket(destAdd);
//			DataOutputStream out;
			int id = Server.getSocketID(server);
			try {
//				out = new DataOutputStream(sk.getOutputStream());
//				out.writeBytes(msg);
				outs[id].writeBytes(msg);
				System.out.println("Forwarded to " + destAdd);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		else{
			//relay to other correspondent server
			System.out.println("Relay to " + server);
//			Socket sk = Server.getSocket(server);
//			System.out.println(sk.getInetAddress());
//			DataOutputStream out;
			int id = Server.getSocketID(server);
//			System.out.println("getSocketID(server)=" + id);
//			int id = 0;
			try {
//				out = new DataOutputStream(sk.getOutputStream());
//				out = new DataOutputStream(Server.sockets[id].getOutputStream());
//				Server.out[id].writeBytes(msg);
				outs[id].writeBytes(msg);
//				Server.buffers[id].queue.add(msg);
//				System.out.println("thit rau " + Server.buffers[id].queue.remove());
				System.out.println("Relay done!");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
}
